package org.xxpay.pay.channel.weihui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;
import org.xxpay.common.http.HttpRequestTemplate;
import org.xxpay.common.util.HtmlUtils;
import org.xxpay.common.util.sign.SignUtils;
import org.xxpay.common.util.str.StringUtils;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.AmountUtil;
import org.xxpay.core.common.util.DateUtil;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;
import sun.security.krb5.internal.crypto.Aes128;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

@Service
public class WeihuiPaymentService extends BasePayment {

    private final static String logPrefix = "【微汇支付支付】";
    @Override
    public String getChannelName() {
        return WeihuiConfig.CHANNEL_NAME;
    }

    /**
     * 支付下单
     * @param payOrder
     * @return
     */
    @Override
    public JSONObject pay(PayOrder payOrder){
        return doOrderReq(payOrder);
    }

    /**
     * 查询订单
     * @param payOrder
     * @return
     */
    @Override
    public JSONObject query(PayOrder payOrder){
        JSONObject retObj = buildRetObj();
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_FAIL);
        retObj.put(PayConstant.RESULT_PARAM_ERRDES, "没有订单查询接口");
        return retObj;
    }

    //下单具体实现
    private JSONObject doOrderReq(PayOrder payOrder){
        WeihuiConfig config = new WeihuiConfig(getPayParam(payOrder));
        Map<String,Object> params = Maps.newHashMap();
        //设置请求参数
        setParams(payOrder, config, params);
        _log.info("{}请求数据:{}", getChannelName(), JSON.toJSONString(params));
        //将订单改为支付中
        rpcCommonService.rpcPayOrderService.updateStatus4Ing(payOrder.getPayOrderId(), null);
        JSONObject retObj = new JSONObject();
        String payFrom = HtmlUtils.form(config.getRequestUrl() + WeihuiConfig.REQ_PAY_URL_FIXX, HtmlUtils.POST, params);
        return buildPayResultOfForm(retObj,payOrder,payFrom);

   
    } 

    //设置请求参数
    private void setParams(PayOrder payOrder, WeihuiConfig config,Map<String,Object> params){
        Map<String, String> jpar = new LinkedHashMap<String, String>();
        jpar.put("apikey", config.getPrivateKey());
        jpar.put("order_id", payOrder.getPayOrderId());
        jpar.put("order_price", AmountUtil.convertCent2Dollar(String.valueOf(payOrder.getAmount())));
        jpar.put("notify_url", payConfig.getNotifyUrl(getChannelName()));
        jpar.put("return_url", payOrder.getReturnUrl());
        jpar.put("type", config.getPayMode());
        jpar.put("sign", getSign(jpar, config.getPrivateKey()));
        String content = JSON.toJSONString(jpar);
        String AESKey = "vhuifujiakey2000";
        String AESVi = "vhuifue5ffiv2000";
        String contentStr = AESUtil.encrypt(content,AESKey,AESVi).toUpperCase();
        _log.info("{}请求源数据:{}", getChannelName(), content);
        _log.info("{}AES加密前数据:{}", getChannelName(), content);
        _log.info("{}AESKey:{}", getChannelName(), AESKey);
        _log.info("{}AESVi:{}", getChannelName(), AESVi);
        _log.info("{}AES加密后数据:{}", getChannelName(), contentStr);
        params.put("content", contentStr);
    }
    //获取sign
    private String getSign(Map<String,String> params,String key){
        String signTxt = SignUtils.parameterText(params);
        _log.info("{}待签名字符串：{}", logPrefix, signTxt);
        String sign = SignUtils.MD5.createSign(signTxt, "", SignUtils.CHARSET_UTF8).toUpperCase();
        return sign;
    }

}

